﻿const express = require('express');
const router = express.Router();
const NoticeModel = require('../models/notice');
// 获取通知列表 GET notice?class=xxx
router.get('/', function(req, res, next) {
    let classGuid = req.query.class;
    let n = req.query.index || 0;
    let m = req.query.num || 100000;
    let state = req.query.state || 0;
    NoticeModel.getNotices(classGuid, n, m, state, function(vals) {
        console.log(vals)
        res.send({ status: 1, data: vals });
    });
});
// 添加（发布）通知 POST notice
router.post('/', function(req, res, next) {
    let data = {
        title: req.body.title,
        content: req.body.content,
        object: req.body.object,
        classGuid: req.body.classGuid
    };
    NoticeModel.addNotice(data, function(vals) {
        res.send({ status: 1, msg: 'ok' });
    });
});
// 修改通知 PUT notice
router.put('/:guid', function(req, res, next) {
    let data = {
        noticeGuid: req.params.guid,
        title: req.body.title,
        content: req.body.content,
        object: req.body.object
    };
    NoticeModel.updateNotice(data, function(vals) {
        res.send({ status: 1, msg: 'ok' });
    });
});

// 删除通知 DELETE notice/:guid
router.delete('/:guid', function(req, res, next) {
    let noticeGuid = req.params.guid;
    NoticeModel.delNotice(noticeGuid, function(vals) {
        res.send({ status: 1, msg: 'ok' });
    });
});

// 恢复通知 PUT notice/:guid
router.put('/recovery/:guid', function(req, res, next) {
    let noticeGuid = req.params.guid;
    NoticeModel.recoverNotice(noticeGuid, function(vals) {
        res.send({ status: 1, msg: 'ok' });
    });
});

// 已读 PUT read
router.post('/read', function(req, res, next) {
    let data = {
        FK_NoticeGuid: req.body.FK_NoticeGuid,
        FK_UserGuid: req.body.FK_UserGuid
    }
    console.log(data)
    NoticeModel.read(data, function(vals) {
        res.send({ status: 1, msg: 'ok' });
    });
});

// 用学生guid获取通知列表 get 
router.get('/getNoticeByUser', function(req, res, next) {
    let data = {
        FK_ClassGuid: req.query.FK_ClassGuid,
        FK_UserGuid: req.query.FK_UserGuid,
        state: req.query.state || 0
    }
    NoticeModel.getNoticeByUser(data, function(vals) {
        res.send(vals);
    });
});

// 是否有未读 get 
router.get('/Unread', function(req, res, next) {
    let data = {
        FK_ClassGuid: req.query.FK_ClassGuid,
        FK_UserGuid: req.query.FK_UserGuid
    }
    NoticeModel.Unread(data, function(vals) {
        res.send(vals);
    });
});
module.exports = router;